Sprite Byte: Shooting Multiple Missiles

Level: Beginner - Intermediate

by Alyce Watson [http://alycesrestaurant.com/]

Home

Using QCard DLL - Lesson 3 - Watson

Sprite Byte - Watson

Programming a Word Game - Terra

Maven Puzzle Contest - Terra

Adding an Icon to the Taskbar - Lewis

Beginning Programming IX - Moore

Rendering Solid Objects - Nally


Submission Guildlines

Newsletter Help

Index


Sprite Bytes are small tutorials that address a single method to be used with sprites in Liberty BASIC. This Sprite Byte is about shooting multiple missiles. It is a follow-up tutorial to the Sprite Byte in Newsletter #122. The program accompanying this tutorial is called "shoot3.bas" and is included in the zip archive of this issue.

Lots of Ammunition!

It isn't realistic when a ship must wait until a missile is offscreen before it can file another missile. We can add as many missiles as we like and manage them all in a FOR...NEXT loop. For the demo, we'll use 10 missiles. Do we need to load 10 bitmaps for these missiles? If all missiles are identical, then the answer is NO! We only need to load the bitmap one time, but we can use it for as many sprites as we like. We can do something like this:

loadbmp "shot","shoot.bmp"

#1 "addsprite shot1 shot"
#1 "addsprite shot2 shot"
#1 "addsprite shot3 shot"
#1 "addsprite shot4 shot"
#1 "addsprite shot5 shot"
#1 "addsprite shot6 shot"
#1 "addsprite shot7 shot"
#1 "addsprite shot8 shot"
#1 "addsprite shot9 shot"
#1 "addsprite shot10 shot"

This method adds some lines of code. It doesn't add much for 10 missiles, but what if we wanted 20, or 100? There are better ways to do this!

Getting Loopy with Missiles

We can add as many missiles as we want and it only takes three lines of code! We do this in a FOR...NEXT loop. It looks like this:

    for i = 1 to 10
        #1 "addsprite shot"+str$(i)+" shot"
    next

Notice how we accomplished this. We concatenated the sprite names with the value of the counter variable. The first shot is then "shot" + "1", making it "shot1." The second shot is "shot" + "2", making it "shot2" and so on. (To "concatenate" is to add two or more strings together.)

	#1 "addsprite shot"+str$(i)+" shot"

Not only does this use less code now, but it also makes it much easier to manage the missiles later in the program!

Keeping Track of Mulitple Missiles

We have 10 missiles available. How do we know which one to fire when the user presses the "up" arrow key? In this demo, we set a variable equal to the index of the last shot fired.

    activeShot=0    'var to hold number of most recently fired missile

When the user tries to shoot a missile, we increment the value of this variable by 1, so the next missile is fired. We only have 10 missiles, so when the counter exceeds 10, we set it back to 1.

    activeShot=activeShot+1
    if activeShot>10 then activeShot=1

We get the coordinates to start the missile by checking the location of the ship and adjusting accordingly: (See issue #122 for details of firing a missile from a user-controlled sprite.)

    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY

Now the variable activeShot contains the index of the missile to fire. We use that value when activating the missile sprite.

    #1 "spritexy shot"+str$(activeShot)+" ";shotX;" ";shotY

After setting the coordinates, we tell the sprite to travel to the top of the screen and to fire an event when it gets there.

[goShoot]
    #1 "spritetravelxy shot"+str$(activeShot)+" ";shotX;"  0 5 [checkShoot]"

Here is the entire code routine that is activated when the user presses a key to fire a missile:

[goShoot]
    activeShot=activeShot+1
    if activeShot>10 then activeShot=1
    #1 "spritexy? ship x y"
    'start shot near center of ship
    shotX=x+15:shotY=maxY
    'set shot to travel to top of screen and
    'fire event when it gets there
    #1 "spritexy shot"+str$(activeShot)+" ";shotX;" ";shotY
    #1 "spritetravelxy shot"+str$(activeShot)+" ";shotX;"  0 5 [checkShoot]"
    playwave "laser.wav", async
    return

Removing Missiles When They Go Offscreen

Any time a missile gets to the top of the screen, the program executes the [checkShoot] routine. How do we know which missile caused the routine to be called? We want to be sure to remove only the sprite at the top of the screen! It is time for another FOR...NEXT loop. (Are you beginning to see why we chose this method over the one that would hard code each missile individually?)

We use the "spritexy?" command in a loop to find the missile that is at the top of the screen. When the "y" coordinate returned by this command is equal to, or less than 0, we know we've found a missile that must be removed.

[checkShoot]
    #1 "spritexy? shot"+str$(i)+" sx sy"
    if sy<=0 then
        'code to remove goes here
    end if

To remove the sprite, we simply move it to an unseen portion of the screen. Here, we've used the coordinates -100,-100. The missile is not visible to the player, nor will it trigger events or collisions in the game.

Detecting Missile Hits

A future Sprite Byte will cover the management of missiles hitting targets.

Demo

See "shoot3.bas" in the zip archive of this newsletter.


Home

Using QCard DLL - Lesson 3 - Watson

Sprite Byte - Watson

Programming a Word Game - Terra

Maven Puzzle Contest - Terra

Adding an Icon to the Taskbar - Lewis

Beginning Programming IX - Moore

Rendering Solid Objects - Nally


Submission Guildlines

Newsletter Help

Index